package com.ysd.util;

import com.ysd.lis.entity.AgeResultDto;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.Period;

public class AgeUtils {
    public AgeResultDto CalcAge(String ageStr) {
        AgeResultDto res = new AgeResultDto();
        String[] ageArray = new String[7];
        ageArray[0] = "-1";//岁
        ageArray[1] = "-1";//月
        ageArray[2] = "-1";//周
        ageArray[3] = "-1";//天
        ageArray[4] = "-1";//时
        ageArray[5] = "-1";//分
        ageArray[6] = "-1";//秒

        ageStr = ageStr.replaceAll("年", "岁").replaceAll("小时", "时").replaceAll("日", "天");

        String lastStr = ageStr;

        /*计算年*/
        String[] arr1 = lastStr.split("岁");
        if (arr1.length == 2) {
            ageArray[0] = arr1[0];
            lastStr = arr1[1];
        } else {
            if (arr1[0].contains("岁") || arr1[0].contains("月") || arr1[0].contains("周") || arr1[0].contains("天") || arr1[0].contains("时") || arr1[0].contains("分") || arr1[0].contains("秒")) {
                lastStr = arr1[0];
            } else {
                lastStr = "-1";
                ageArray[0] = arr1[0];
            }
        }

        /*计算月*/
        String[] arr2 = lastStr.split("月");
        if (arr2.length == 2) {
            ageArray[1] = arr2[0];
            lastStr = arr2[1];
        } else {
            if (arr2[0].contains("岁") || arr2[0].contains("月") || arr2[0].contains("周") || arr2[0].contains("天") || arr2[0].contains("时") || arr2[0].contains("分") || arr2[0].contains("秒")) {
                lastStr = arr2[0];
            } else {
                lastStr = "-1";
                ageArray[1] = arr2[0];
            }
        }

        /*计算周*/
        String[] arr3 = lastStr.split("周");
        if (arr3.length == 2) {
            ageArray[2] = arr3[0];
            lastStr = arr3[1];
        } else {
            if (arr3[0].contains("岁") || arr3[0].contains("月") || arr3[0].contains("周") || arr3[0].contains("天") || arr3[0].contains("时") || arr3[0].contains("分") || arr3[0].contains("秒")) {
                lastStr = arr3[0];
            } else {
                lastStr = "-1";
                ageArray[2] = arr3[0];
            }
        }

        /*计算天*/
        String[] arr4 = lastStr.split("天");
        if (arr4.length == 2) {
            ageArray[3] = arr4[0];
            lastStr = arr4[1];
        } else {
            if (arr4[0].contains("岁") || arr4[0].contains("月") || arr4[0].contains("周") || arr4[0].contains("天") || arr4[0].contains("时") || arr4[0].contains("分") || arr4[0].contains("秒")) {
                lastStr = arr4[0];
            } else {
                lastStr = "-1";
                ageArray[3] = arr4[0];
            }
        }

        /*计算时*/
        String[] arr5 = lastStr.split("时");
        if (arr5.length == 2) {
            ageArray[4] = arr5[0];
            lastStr = arr5[1];
        } else {
            if (arr5[0].contains("岁") || arr5[0].contains("月") || arr5[0].contains("周") || arr5[0].contains("天") || arr5[0].contains("时") || arr5[0].contains("分") || arr5[0].contains("秒")) {
                lastStr = arr5[0];
            } else {
                lastStr = "-1";
                ageArray[4] = arr5[0];
            }
        }

        /*计算分*/
        String[] arr6 = lastStr.split("分");
        if (arr6.length == 2) {
            ageArray[5] = arr6[0];
            lastStr = arr6[1];
        } else {
            if (arr6[0].contains("岁") || arr6[0].contains("月") || arr6[0].contains("周") || arr6[0].contains("天") || arr6[0].contains("时") || arr6[0].contains("分") || arr6[0].contains("秒")) {
                lastStr = arr6[0];
            } else {
                lastStr = "-1";
                ageArray[5] = arr6[0];
            }
        }

        /*计算秒*/
        String[] arr7 = lastStr.split("秒");
        if (arr7.length == 2) {
            ageArray[6] = arr7[0];
            lastStr = arr7[1];
        } else {
            if (arr7[0].contains("岁") || arr7[0].contains("月") || arr7[0].contains("周") || arr7[0].contains("时") || arr7[0].contains("分") || arr7[0].contains("秒")) {
                lastStr = arr7[0];
            } else {
                lastStr = "-1";
                ageArray[6] = arr7[0];
            }
        }
        try {
            int i = 0;
            for (int j = 0; j < ageArray.length; j++) {
                if (!"-1".equals(ageArray[j])) {
                    i = j;
                    break;
                }
            }


            if (i == 0) {
                res.setAge(Integer.parseInt(ageArray[i]));
                res.setAgeUnit("岁");
                /*查找第二年龄*/
                for (int j = 1; j < ageArray.length; j++) {
                    boolean hasValue = false;
                    if (!"-1".equals(ageArray[j])) {
                        switch (j) {
                            case 1:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("月");
                                hasValue = true;
                                break;
                            case 2:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("周");
                                hasValue = true;
                                break;
                            case 3:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("天");
                                hasValue = true;
                                break;
                            case 4:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("时");
                                hasValue = true;
                                break;
                            case 5:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("分");
                                hasValue = true;
                                break;
                            case 6:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("秒");
                                hasValue = true;
                                break;
                        }
                        if (hasValue) {
                            break;
                        }
                    }
                }
            }
            if (i == 1) {
                res.setAge(Integer.parseInt(ageArray[i]));
                res.setAgeUnit("月");
                /*查找第二年龄*/
                for (int j = 2; j < ageArray.length; j++) {
                    boolean hasValue = false;
                    if (!"-1".equals(ageArray[j])) {
                        switch (j) {
                            case 1:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("月");
                                hasValue = true;
                                break;
                            case 2:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("周");
                                hasValue = true;
                                break;
                            case 3:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("天");
                                hasValue = true;
                                break;
                            case 4:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("时");
                                hasValue = true;
                                break;
                            case 5:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("分");
                                hasValue = true;
                                break;
                            case 6:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("秒");
                                hasValue = true;
                                break;
                        }
                        if (hasValue) {
                            break;
                        }
                    }
                }
            }
            if (i == 2) {
                res.setAge(Integer.parseInt(ageArray[i]));
                res.setAgeUnit("周");
                /*查找第二年龄*/
                for (int j = 3; j < ageArray.length; j++) {
                    boolean hasValue = false;
                    if (!"-1".equals(ageArray[j])) {
                        switch (j) {
                            case 1:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("月");
                                hasValue = true;
                                break;
                            case 2:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("周");
                                hasValue = true;
                                break;
                            case 3:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("天");
                                hasValue = true;
                                break;
                            case 4:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("时");
                                hasValue = true;
                                break;
                            case 5:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("分");
                                hasValue = true;
                                break;
                            case 6:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("秒");
                                hasValue = true;
                                break;
                        }
                        if (hasValue) {
                            break;
                        }
                    }
                }
            }
            if (i == 3) {
                res.setAge(Integer.parseInt(ageArray[i]));
                res.setAgeUnit("天");
                /*查找第二年龄*/
                for (int j = 4; j < ageArray.length; j++) {
                    boolean hasValue = false;
                    if (!"-1".equals(ageArray[j])) {
                        switch (j) {
                            case 1:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("月");
                                hasValue = true;
                                break;
                            case 2:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("周");
                                hasValue = true;
                                break;
                            case 3:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("天");
                                hasValue = true;
                                break;
                            case 4:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("时");
                                hasValue = true;
                                break;
                            case 5:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("分");
                                hasValue = true;
                                break;
                            case 6:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("秒");
                                hasValue = true;
                                break;
                        }
                        if (hasValue) {
                            break;
                        }
                    }
                }
            }
            if (i == 4) {
                res.setAge(Integer.parseInt(ageArray[i]));
                res.setAgeUnit("时");
                /*查找第二年龄*/
                for (int j = 5; j < ageArray.length; j++) {
                    boolean hasValue = false;
                    if (!"-1".equals(ageArray[j])) {
                        switch (j) {
                            case 1:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("月");
                                hasValue = true;
                                break;
                            case 2:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("周");
                                hasValue = true;
                                break;
                            case 3:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("天");
                                hasValue = true;
                                break;
                            case 4:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("时");
                                hasValue = true;
                                break;
                            case 5:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("分");
                                hasValue = true;
                                break;
                            case 6:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("秒");
                                hasValue = true;
                                break;
                        }
                        if (hasValue) {
                            break;
                        }
                    }
                }
            }
            if (i == 5) {
                res.setAge(Integer.parseInt(ageArray[i]));
                res.setAgeUnit("分");
                /*查找第二年龄*/
                for (int j = 6; j < ageArray.length; j++) {
                    boolean hasValue = false;
                    if (!"-1".equals(ageArray[j])) {
                        switch (j) {
                            case 1:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("月");
                                hasValue = true;
                                break;
                            case 2:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("周");
                                hasValue = true;
                                break;
                            case 3:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("天");
                                hasValue = true;
                                break;
                            case 4:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("时");
                                hasValue = true;
                                break;
                            case 5:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("分");
                                hasValue = true;
                                break;
                            case 6:
                                res.setSecAge(Integer.parseInt(ageArray[j]));
                                res.setSecAgeUnit("秒");
                                hasValue = true;
                                break;
                        }
                        if (hasValue) {
                            break;
                        }
                    }
                }
            }
            if (i == 6) {
                res.setAge(Integer.parseInt(ageArray[i]));
                res.setAgeUnit("秒");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }

    public AgeResultDto calcAgeByBirthday(Timestamp birthdayStr) {
        AgeResultDto res = new AgeResultDto();
        Timestamp birthdayTimestamp = new Timestamp(birthdayStr.getTime()); // 这是一个 Timestamp 示例

        LocalDate birthday = birthdayTimestamp.toLocalDateTime().toLocalDate();

        LocalDate currentDate = LocalDate.now();
        Period period = Period.between(birthday, currentDate);
        int age = period.getYears();
        res.setAge(age);
        res.setAgeUnit("岁");
        return res;
    }
}
